﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web.UI.WebControls;

public partial class ACPanel_NodePhuongTien_QLLoaiPhuongTien : System.Web.UI.Page
{
    public static SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["defaultConnection"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGrid("Id", false);
        }
    }


    /// <summary>
    /// Method : FillGrid 
    /// Use : Bind data to gridview
    /// </summary>
    /// <param name="SortExpression"></param>
    /// <param name="IsSorting"></param>
    public void FillGrid(string SortExpression, bool IsSorting)
    {
        var dt = new DataTable();
        dt.Columns.Add("Id");
        dt.Columns.Add("Ten");
        dt.Columns.Add("NgayCapNhat");
        dt.Columns.Add("TrangThai", typeof(bool));

        // Lay chuoi ket noi CSDL
        // var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        Connection.Open();
        const string query = "SELECT * FROM [VYC-07] ORDER BY NgayCapNhat DESC";
        var dataAdapter = new SqlDataAdapter(query, Connection);
        //DataSet dataSet = new DataSet();
        //dataAdapter.Fill(dataSet, "VYC00");
        dataAdapter.Fill(dt);
        Connection.Close(); // dong ket noi       

        var dv = dt.DefaultView;
        if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
        {
            if (IsSorting)
            {
                if (ViewState["SortExpression"].ToString() == SortExpression)
                {
                    if (ViewState["SortDirection"].ToString() == "ASC")
                        ViewState["SortDirection"] = "DESC";
                    else
                        ViewState["SortDirection"] = "ASC";
                }
                else
                {
                    ViewState["SortExpression"] = SortExpression;
                    ViewState["SortDirection"] = "ASC";
                }
            }
        }
        else
        {
            ViewState["SortExpression"] = SortExpression;
            ViewState["SortDirection"] = "ASC";
        }

        dv.Sort = ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        gv.DataSource = dv.ToTable();
        gv.DataBind();
    }

    /// <summary>
    /// Event OnPageIndexChanging="gv_PageIndexChanging" on GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        FillGrid(ViewState["SortExpression"].ToString(), false);
    }

    /// <summary>
    /// Event OnSorting="gv_Sorting" on GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        FillGrid(e.SortExpression, true);
    }    
}